package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-28 11:28
 * @Description:
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;


    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpServletRequest request){
        //产生一个随机验证码
        String code = ValidateCodeUtils.generateValidateCode4String(4);
        //将验证码放入sesssion中,用来注册时判断验证码是否正确
        request.getSession().setAttribute("code",code);
        //使用阿里短信发送出去
        //返回验证码
        return Result.ok(code);
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo,HttpServletRequest request){
        //获取四个表单项
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //判断四个表单项是否为null或者为''（防止跳过页面直接访问后端）
        if(StringUtils.isEmpty(nickName)||StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(password) ||StringUtils.isEmpty(code)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //判断验证码是否正确
        String codestr = (String) request.getSession().getAttribute("code");
        if(StringUtils.isEmpty(codestr)||!codestr.equals(code)){
            return Result.build(null,ResultCodeEnum.CODE_ERROR);
        }
        //判断手机号是否已经存在
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(userInfo != null){
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

        //注册成功
        userInfo = new UserInfo();

        userInfo.setNickName(nickName);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setPhone(phone);
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);

        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request){
        //获取两个表单项
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //判断是否为空
        if(StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(password)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //按照phone进行查询
        UserInfo userInfo = userInfoService.getByPhone(loginVo.getPhone());
        if(userInfo == null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //判断密码是否正确
        if(!userInfo.getPassword().equals(MD5.encrypt(password))){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //判断用户是否已经禁用
        if(userInfo.getStatus() == 0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //将用户储存在session中,用来登陆之后的改变页面
        request.getSession().setAttribute("USER",userInfo);
        //一切正常,返回ok,要携带用户的昵称过去
        Map map = new HashMap<>();
        map.put("nickName", userInfo.getNickName());
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request){
        //结束当前session
        request.getSession().invalidate();//结束session,开启一个新的session
        //request.getSession().removeAttribute("USER");//session不变,只是清除其中的指定数据
        return Result.ok();
    }
}
